@file:Suppress("unused")

package com.example.base.tools

import android.app.Activity
import android.app.Application
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import java.util.concurrent.CopyOnWriteArrayList

/**  App 界面管理类  **/
@RequiresApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
class UIManager : Application.ActivityLifecycleCallbacks {

    companion object {
        //要用CopyOnWriteArrayList 线程安全的
        private val activities = CopyOnWriteArrayList<Activity>()
        fun closeApp() {
            activities.forEach {
                if (it.isFinishing.not()) it.finish()
            }
        }

        /**  获取当前栈顶的 Activity  **/
        fun top() = activities[activities.size - 1]

        /**  获取当前 Activity 栈  **/
        fun getStack() = activities
    }

    override fun onActivityStarted(activity: Activity) {

    }

    override fun onActivityResumed(activity: Activity) {

    }

    override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
        activities.add(activity)
    }

    override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {

    }

    override fun onActivityPaused(activity: Activity) {

    }

    override fun onActivityStopped(activity: Activity) {

    }

    override fun onActivityDestroyed(activity: Activity) {
        activities.remove(activity)
    }

}